﻿using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace Vehicle.Common
{


    public class MessageHelper
    {
        #region -----脚本信息提示类-----
        /// <summary>
        /// 脚本提示类构造函数
        /// </summary>
        public MessageHelper()
        {
        }
        /// <summary>
        /// 提示信息
        /// </summary>
        /// <param name="msg">显示信息</param>
        public static void Alert(string msg)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<script language='javascript'>");
            str.Append("alert('" + msg + "')");
            str.Append("</script>");
            RegisterClientScript(str.ToString());
        }
        /// <summary>
        /// 提示信息并返回
        /// </summary>
        /// <param name="msg">显示信息</param>
        public static void AlertBack(string msg)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<script language='javascript'>");
            str.Append("alert('" + msg + "');");
            str.Append("history.back();");
            str.Append("</script>");
            RegisterClientScript(str.ToString());
        }

        /// <summary>
        /// 提示信息并关闭窗口
        /// </summary>
        /// <param name="msg">提示信息</param>
        public static void AlertCloseThisWin(string msg)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<script language='javascript'>");
            str.Append("alert('" + msg + "');");
            str.Append("this.window.opener = null; ");
            str.Append("this.window.close();");
            str.Append("</script>");
            RegisterClientScript(str.ToString());
        }
        /// <summary>
        /// 提示信息并关闭当前窗口，刷新父窗口
        /// </summary>
        /// <param name="msg">提示信息</param>
        /// <param name="theUrl">父窗口地址</param>
        public static void AlertCloseThisWinReload(string msg, string theUrl)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<script language='javascript'>");
            str.Append("alert('" + msg + "');");
            str.Append(("dialogArguments.location.href = '" + theUrl + "';"));
            str.Append("this.window.close();");
            str.Append("</script>");
            RegisterClientScript(str.ToString());
        }
        /// <summary>
        /// 提示信息并关闭总窗口
        /// </summary>
        /// <param name="msg">提示信息</param>
        public static void AlertCloseTopWin(string msg)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<script language='javascript'>");
            str.Append("alert('" + msg + "');");
            str.Append("this.window.opener = null; ");
            str.Append("top.window.close();");
            str.Append("</script>");
            RegisterClientScript(str.ToString());
        }
        /// <summary>
        /// 提示信息并使光标到指定位置
        /// </summary>
        /// <param name="msg">提示信息</param>
        /// <param name="strObjName">客户端WEB控件</param>
        public static void AlertFocus(string msg, string strObjName)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<script language='javascript'>");
            str.Append("alert('" + msg + "');");
            str.Append(("" + strObjName + ".focus();"));
            str.Append("</script>");
            RegisterClientScript(str.ToString());
        }
        /// <summary>
        /// 提示信息并转到指定页面
        /// </summary>
        /// <param name="msg">提示消息</param>
        /// <param name="url">转向的URL</param>
        public static void AlertRedirect(string msg, string url)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<script language='javascript'>");
            str.Append("alert('" + msg + "');");
            str.Append(("location.href='" + url + "'"));
            str.Append("</script>");
            RegisterClientScript(str.ToString());

        }
        /// <summary>
        /// 打印信息
        /// </summary>
        /// <param name="msg">提示消息</param>
        public static void Write(string msg)
        {
            System.Web.HttpContext.Current.Response.Write(msg.ToString());

        }
        /// <summary>
        /// 转到指定页面
        /// </summary>
        /// <param name="url">页面地址</param>
        public static void Redirect(string url)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<script language='javascript'>");
            str.Append(("location.href='" + url + "'"));
            str.Append("</script>");
            RegisterClientScript(str.ToString());

        }

        /// <summary>
        /// 注册脚本
        /// </summary>
        /// <param name="str">脚本代码</param>
        public static void RegisterClientScript(string str)
        {
            System.Web.UI.Page page = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
            if (page != null)
            {
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), System.Guid.NewGuid().ToString(), str);

            }
            else
            {
                System.Web.HttpContext.Current.Response.Write(str.ToString());
                System.Web.HttpContext.Current.Response.End();
            }

        }
        #endregion
    }
}

